package cn.cdeden.sys.utils.domain;

import cn.cdeden.sys.enums.LoginMethod;
//import com.cdyd.ecm.sys.enums.LoginMethod;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ProjectName : ecm_svn
 * @Package : com.cdyd.ecm.core.sys.domain.config
 * @ClassName : UserLoginInfo
 * @Author : Yu.
 * @Description : // 验证用户拥有那些登录方式配置
 * @Date : 2024/3/12 16:43
 * @Version : 1.0
 */
//@Data
@Getter
public class UserLoginInfo {


    private boolean username; //用户登录
    private boolean email; //邮箱
    private boolean phone; //电话
    private boolean jobNumber; //工号
    private boolean SSO; //单点
    @Setter
    private boolean ad; // ad域
    @Setter
    private boolean wxscan;// 企业微信
    @Setter
    private boolean OAuth;// OA系统

    public UserLoginInfo(){
        this.username = true;
    }



    public UserLoginInfo(List<String> userLoginInfo){
        Map<LoginMethod, Boolean> loginMethodsMap = new EnumMap<>(LoginMethod.class);
        for (LoginMethod method : LoginMethod.values()) {
            loginMethodsMap.put(method, false);
        }
        if (CollectionUtils.isNotEmpty(userLoginInfo)) {
            for (String methodValue : userLoginInfo) {
                LoginMethod method = LoginMethod.fromCode(methodValue);
                if (method == null) {
                    continue;
                }
                loginMethodsMap.put(method, true);
            }
            this.username = loginMethodsMap.get(LoginMethod.USERNAME);
            this.email = loginMethodsMap.get(LoginMethod.EMAIL);
            this.phone = loginMethodsMap.get(LoginMethod.PHONE);
            this.jobNumber = loginMethodsMap.get(LoginMethod.JOB_NUMBER);
            this.SSO = loginMethodsMap.get(LoginMethod.SSO);
            this.wxscan = loginMethodsMap.get(LoginMethod.WXSCAN);
            this.ad = loginMethodsMap.get(LoginMethod.AD);
            this.OAuth = loginMethodsMap.get(LoginMethod.OAuth);
            // 根据需要，你可以在这里添加一个检查，确保至少有一种登录方式是启用的

        } else {
            // 如果需要，可以在此处抛出异常或记录日志，表明 userLoginInfo 不应为空
        }

        boolean hasAtLeastOneEnabled = loginMethodsMap.values().stream().anyMatch(Boolean::booleanValue);

        if (!hasAtLeastOneEnabled) {
            this.username = true;
        }
    }



    public UserLoginInfo(List<String> userLoginInfo,String s){
        Map<LoginMethod, Boolean> loginMethodsMap = new EnumMap<>(LoginMethod.class);
        for (LoginMethod method : LoginMethod.values()) {
            loginMethodsMap.put(method, false);
        }
        if (CollectionUtils.isNotEmpty(userLoginInfo)) {
            for (String methodValue : userLoginInfo) {
                LoginMethod method = LoginMethod.fromCode(methodValue);
                if (method == null) {
                    continue;
                }
                loginMethodsMap.put(method, true);
            }
            this.username = loginMethodsMap.get(LoginMethod.USERNAME);
            this.email = loginMethodsMap.get(LoginMethod.EMAIL);
            this.phone = loginMethodsMap.get(LoginMethod.PHONE);
            this.jobNumber = loginMethodsMap.get(LoginMethod.JOB_NUMBER);
            this.SSO = loginMethodsMap.get(LoginMethod.SSO);
            this.wxscan = loginMethodsMap.get(LoginMethod.WXSCAN);
            this.ad = loginMethodsMap.get(LoginMethod.AD);
            this.OAuth = loginMethodsMap.get(LoginMethod.OAuth);
            // 根据需要，你可以在这里添加一个检查，确保至少有一种登录方式是启用的

        } else {
            // 如果需要，可以在此处抛出异常或记录日志，表明 userLoginInfo 不应为空
        }

        boolean hasAtLeastOneEnabled = loginMethodsMap.values().stream().anyMatch(Boolean::booleanValue);

        if (!hasAtLeastOneEnabled) {
            throw new RuntimeException("最少需要一个登录方式");

        }
    }

    public List<String> getList(){
        List<String> list = new ArrayList<>();
        if (this.username) {
            list.add(LoginMethod.USERNAME.getCode());
        }
        if (this.email) {
            list.add(LoginMethod.EMAIL.getCode());
        }
        if (this.jobNumber) {
            list.add(LoginMethod.JOB_NUMBER.getCode());
        }
        if (this.SSO) {
            list.add(LoginMethod.SSO.getCode());
        }
        if (this.wxscan) {
            list.add(LoginMethod.WXSCAN.getCode());
        }
        if (this.ad) {
            list.add(LoginMethod.AD.getCode());
        }
        return list.size() > 0 ? list : Arrays.asList(LoginMethod.USERNAME.getCode());
    }

    public List<String> getEnabledLoginMethods() {
        List<String> collect = Arrays.stream(LoginMethod.values())
                .filter(method -> isOpen(method))
                .map(LoginMethod::getCode)
                .collect(Collectors.toList());

        return collect.size() > 0 ? collect : Arrays.asList(LoginMethod.USERNAME.getCode());
    }


    /**
     * 根据指定的登录方式判断该方式是否开启。
     *
     * @param method 登录方式枚举，表示要查询的登录方式。
     * @return boolean 返回true表示该登录方式开启，返回false表示该登录方式关闭。
     */
    public boolean isOpen(LoginMethod method){
//        我想传进来一个LoginMethod，然后根据LoginMethod返回true或者false，true表示该登录方式是开启的，false表示该登录方式是关闭的
        switch (method){
            case USERNAME:
                return username;
            case EMAIL:
                return email;
            case PHONE:
                return phone;
            case JOB_NUMBER:
                return jobNumber;
            case SSO:
                return SSO;
            case WXSCAN:
                return wxscan;
            case AD:
                return ad;
            case OAuth:
                return OAuth;
            default:
                return false;
        }
    }



    /**
     * @author Yu.
     * @Description // 判断
     * @Date 2024/5/21 18:47
     * @return boolean
     **/
    public boolean isValidCombination() {
        int enabledCount = 0;
        if (wxscan) {
            enabledCount++;
        }
        if (ad) {
            enabledCount++;
        }
        if (OAuth) {
            enabledCount++;
        }

        return enabledCount <= 1;
    }

}
